<?php

namespace App\Repository\Commit;

use App\Models\Admin\AdminUsers;
use App\Models\Dept as model;
use App\Repository\BaseRepository;

class DeptRepository extends BaseRepository
{
    public function __construct(model $model)
    {
        $this->model = $model;
        parent::__construct();
    }

    //列表
    public function index($where)
    {
        $cron = $where;
        $cron['order'] = ['orderby' => 'dept_id', 'orderdir' => 'desc'];
        // $cron['where'] = $where;
        // var_dump($cron,111111111);
        $data = $this->search($this->model, $cron, 1, 9000000);
        if (!empty($data['data'])) {
            $data['data'] = usfirstArr($data['data']);
        }
        return $data;
    }

//  指定部门下的会员
    public function usersOptionList($id)
    {
        $ModelUsers = new AdminUsers();
        $rows = $ModelUsers->where(['status' => 0, 'dept_id' => $id])->select(['id', 'nick_name'])->get();
        return $rows;
    }

    //添加
    public function add($data)
    {
        //必填 字段
        $mustField = [
            'dept_name',
        ];
        $optionalFields = $this->model->getTableField();
        $msgField = [
            'dept_name' => '名称不能为空'
        ];
        if (!empty($data['role_id'])) {
            // var_dump($update['role_id']);
            // $resRole = json_decode($update['role_id'], true);
            $data['role_id'] = implode(',', $data['role_id']);
        }
        $insertData = $this->getRealData($data, $mustField, $optionalFields, $msgField);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }
        //查询父类的参数
        $resP = $this->model->where(['dept_id' => $insertData['parent_id']])->first();
        if (empty($resP)) {
            return $this->returnMsg(100, '未查询到上级');
        }
        $insertData['order_num'] = 0;
        $insertData['ancestors'] = $resP->ancestors . '.' . $insertData['parent_id'] . '.';
        $insertData['shop_id'] = $data->user->shop_id;
        $insertData['create_time'] = date('Y-m-d H:i:s');
        $insertData['create_by'] = $data->user->id;

        $res = $this->model->insert($insertData);
        if ($res) {
            return $this->returnMsg(200, '添加成功');
        }
        return $this->returnMsg(1, '添加失败');
    }

    //删除
    public function del($where)
    {
        $flag = true;
        $msg = '未知错误';
        $code = 1;
        if (empty($where)) {
            $flag = false;
            $msg = '删除条件不能为空';
            $code = 2;
        }
        if ($flag) {
            $res = $this->model->where($where)->get();
            if (!$res) {
                $flag = false;
                $msg = '未查询到数据';
                $code = 3;
            }
        }
        if ($flag) {
            $del = $this->model->where($where)->delete();
            if ($del) {
                $msg = '删除成功';
                $code = 200;
            }
        }
        return $this->returnMsg($code, $msg);
    }

    //修改
    public function edit($where, $data, $isMust = false)
    {
        if (empty($where)) {
            return $this->returnMsg(1, '条件不能为空');
        }
        if (empty($data)) {
            return $this->returnMsg(2, '修改参数不能为空');
        }
        //过滤字段
        //过滤字段
        if (!empty($data['role_id'])) {
            // var_dump($update['role_id']);
            // $resRole = json_decode($update['role_id'], true);
            $data['role_id'] = implode(',', $data['role_id']);
        }
        $optionalFields = $this->model->getTableField();
        $field = $this->getRealData($data, [], $optionalFields, []);
        if (!empty($field['parent_id'])) {
            $resP = $this->model->where(['dept_id' => $field['parent_id']])->first();
            if (empty($resP)) {
                return $this->returnMsg(100, '未查询到上级');
            }
            $field['ancestors'] = $resP->ancestors . '.' . $field['parent_id'] . '.';
        }
        //先查询是否存在

        $res = $this->model->where($where)->update($field);
        if ($isMust && $res) {
            return $this->returnMsg(200, '修改成功');
        }
        if (!$isMust) {
            return $this->returnMsg(200, '修改成功');
        }
        return $this->returnMsg(3, '请重新修改');
    }

    //列表查询
    public function lists($where = [], $page = 1, $limit = 10)
    {
        $res = $this->search($this->model, $where, $page, $limit);

        return $res;
    }

    public function getOne($where)
    {
        return $this->model->where($where)->first();
    }

    public function treeSelect($where = [], $field = [], $shop_id = '')
    {
        if (empty($field)) {
            $field = ['dept_id as id', 'dept_name as label'];
        }
        $return = $this->model->where($where)->select($field)->get();
        if (!empty($return)) {
            foreach ($return as $key => $v) {
                $whereTwo = [
                    'parent_id' => $v->id,
                    'shop_id' => $shop_id
                ];
                $v['children'] = $this->treeSelect($whereTwo, [], $shop_id);
            }
        }
        return $return;
    }
}
